package com.uflower.product.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.uflower.common.exception.WareReduceExcption;
import com.uflower.common.to.WareSkuReduceTo;
import com.uflower.product.feign.UserFeignService;
import com.uflower.product.to.FareTo;
import com.uflower.product.to.SkuHasStockTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.uflower.product.entity.SkuInfoEntity;
import com.uflower.product.service.SkuInfoService;
import com.uflower.common.utils.PageUtils;
import com.uflower.common.utils.R;



/**
 * sku信息
 *
 * @author bridge
 * @email bridgezrq@163.com
 * @date 2022-01-15 14:52:35
 */
@RestController
@RequestMapping("product/skuinfo")
public class SkuInfoController {
    @Autowired
    private SkuInfoService skuInfoService;

    private UserFeignService userFeignService;
    @GetMapping("/fare/{addrId}")
    public R getFare(@PathVariable("addrId") Long addrId){
       FareTo fareTo = skuInfoService.getFare(addrId);
       return R.ok().put("fareTo",fareTo);
    }
    /**
     * 扣减商品数量（加锁）
     *"product/skuinfo"
     */
    @RequestMapping("/reduceStock")
    R orderReduceStock(@RequestBody WareSkuReduceTo reduceTo){
        try{
            skuInfoService.orderReduceStock(reduceTo);
        }catch (WareReduceExcption e){
            return R.error(e.getCode(),e.getMsg());
        }
        return R.ok();
    }
    /**
     * 获得商品价格
     * @param skuId
     * @return
     */
    @GetMapping("/price/{skuId}")
    BigDecimal getPrice(@PathVariable("skuId") Long skuId){
        return skuInfoService.getPriceBySkuId(skuId);
    }

    @PostMapping("/hasStock")
    public R getSkusHasStock(@RequestBody List<Long> skuIds){
       List<SkuHasStockTo> skusHasStockTo = skuInfoService.getSkusHasStock(skuIds);
       return R.ok().put("skusHasStock",skusHasStockTo);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:skuinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = skuInfoService.queryPageByCondition(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     * product/skuinfo/info/{skuId}
     */
    @RequestMapping("/info/{skuId}")
    //@RequiresPermissions("product:skuinfo:info")
    public R info(@PathVariable("skuId") Long skuId){
		SkuInfoEntity skuInfo = skuInfoService.getById(skuId);

        return R.ok().put("skuInfo", skuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:skuinfo:save")
    public R save(@RequestBody SkuInfoEntity skuInfo){
		skuInfoService.save(skuInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:skuinfo:update")
    public R update(@RequestBody SkuInfoEntity skuInfo){
		skuInfoService.updateById(skuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:skuinfo:delete")
    public R delete(@RequestBody Long[] skuIds){
		skuInfoService.removeByIds(Arrays.asList(skuIds));

        return R.ok();
    }

}
